package com.dc.netlib.base

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.LayoutInflaterCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import com.qmuiteam.qmui.skin.QMUISkinLayoutInflaterFactory
import com.qmuiteam.qmui.skin.QMUISkinManager
import java.lang.reflect.ParameterizedType


/**
 *Author: chinadragon
 *Time: 2024/5/7
 */
abstract class BaseActivity<VM : BaseAndroidViewModel<*>, VDB : ViewDataBinding>(
    private val contentViewResId: Int,
    private val variableId: Int = 0,
) : AppCompatActivity() {
    protected lateinit var mViewModel: VM
    protected lateinit var binding: VDB
    protected lateinit var mSkinManager:QMUISkinManager
    override fun onCreate(savedInstanceState: Bundle?) {
        initBefore(savedInstanceState)

        // 使用 QMUISkinLayoutInflaterFactory
        val layoutInflater = LayoutInflater.from(this)
        LayoutInflaterCompat.setFactory2(
            layoutInflater,
            QMUISkinLayoutInflaterFactory(this, layoutInflater)
        )

        super.onCreate(savedInstanceState)
        // 注入 QMUISkinManager
        mSkinManager = QMUISkinManager.defaultInstance(this);
        initViewModel()
        initDataBinding()
        initData()
        initView(savedInstanceState)
        loadData()
        initEvent()

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
//                    View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
    }

    private fun initDataBinding() {
        binding = DataBindingUtil.setContentView(this, contentViewResId)
        if (::binding.isInitialized) {
            binding.apply {
                lifecycleOwner = this@BaseActivity
                setVariable(variableId, mViewModel)
            }
        }
    }

    private fun initViewModel() {

        var type: Class<VM>? = null
        if (this.javaClass.genericSuperclass != null && this.javaClass.genericSuperclass is ParameterizedType) {
            ((this.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VM>).also {
                type = it
            }

        } else if (this.javaClass.superclass != null && this.javaClass.superclass.genericSuperclass != null && this.javaClass.superclass.genericSuperclass is ParameterizedType) {
            type =
                (this.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VM>
        }

        type?.let {
            mViewModel = ViewModelProvider(this)[it]
            if (::mViewModel.isInitialized) {

            }
        }

    }


    open fun initBefore(savedInstanceState: Bundle?) {

    }

    open fun initView(savedInstanceState: Bundle?) {

    }

    /**
     * Called before {@link #initView(savedInstanceState: Bundle?)}
     */
    open fun initData() {

    }

    open fun loadData() {

    }

    open fun initEvent() {

    }
//    @NonNull
//    @Override
//    override fun getDelegate(): AppCompatDelegate {
//        return SkinAppCompatDelegateImpl.get(this, this)
//
//    }

    public override fun onStart() {
        super.onStart()
        mSkinManager.register(this)
    }

    override fun onStop() {
        super.onStop()
        mSkinManager.unRegister(this)
    }
}